<?php

session_start();
include_once($_SERVER['DOCUMENT_ROOT'] ."/include/user/inc.php");
include_once($_SERVER['DOCUMENT_ROOT'] ."/config/mblog_config.php");
include_once($_SERVER['DOCUMENT_ROOT'] ."/core/mblog/weibooauth.php");
include_once($_SERVER['DOCUMENT_ROOT'] ."/include/mblog/inc.php");
include_once($_SERVER['DOCUMENT_ROOT'] ."/include/love/inc.php");
error_reporting(E_ERROR | E_PARSE);

//error_reporting(E_ALL);
//所有的参数
$g_cgival 	= array();
//存储中间数据 非输出数据 可以不用
$g_pro	 	= array();
//要显示的数据 输出数据
$g_show 	= array();

function check_cgi_pro()
{		
	global $g_cgival, $g_pro, $g_show, $config;
	cgi::both($g_cgival["nick"], "nick", "");
	
	//去JS和HTML标签
	$g_cgival['nick'] 	= string::un_script_code($g_cgival['nick']);
	$g_cgival['nick'] 	= string::un_html($g_cgival['nick']);

}


function get_data()
{
	global $g_cgival, $g_pro, $g_show, $config;
	
	$nick = $g_cgival['nick'];

	$c = new WeiboClient( WB_AKEY , WB_SKEY , $_SESSION['last_key']['oauth_token'] , $_SESSION['last_key']['oauth_token_secret']  );
	
	$request_user_info  = $c->verify_credentials(); // done
	$request_nick = $request_user_info["name"];
	
	
	if($request_user_info["error_code"] || !$request_user_info)
	{
		$o = new WeiboOAuth( WB_AKEY , WB_SKEY  );
		header("location:/sina_api/?url=/love/index.php");
		exit;
	}
	
	
	if($nick == $request_nick)
	{
		throw new Exception("你要跟自己那个那个？");
	}
	$answer_user_info  = $c->show_user($nick); // done
	$answer_nick = $answer_user_info["name"];
	
	if($answer_user_info["error_code"])
	{
		throw new Exception($answer_user_info["error"]);
	}
		
	
	
	
	$request_uid = $request_user_info["id"];
	$request_sex = $request_user_info["gender"];
	$answer_uid = $answer_user_info["id"];
	$answer_sex	= $answer_user_info["gender"];
	$db_love_request = new db_love_request();
	$res = $db_love_request->insert_love_request($request_uid,$answer_uid,$request_nick,$answer_nick);
	if(!$res)
	{
		throw new Exception("sorry 数据库出错了");
	}
	
	//当前用户发微博
	$link ="http:\\bless.php$res";
	$text = "@$answer_nick @$request_nick 求婚啦，来祝福吧。11。$link";
	$send_res = $c->update($text);
	
	if($send_res["error_code"])
	{
		throw new Exception($send_res["error"]);
	}
	//当前用户发微博 end
	
	$open_id = $send_res["id"];
	$mid = $send_res["mid"];
	$res1 = $db_love_request->update_love_request_by_id($res,$open_id,$mid,$text);
	if(!$res1)
	{
		throw new Exception("sorry 数据库更新出错了");
	}
	
	//admin用户发微博
	$db_sina_users = new db_sina_users();
	$admin_uid = ADMIN_SEND_UID;
	$admin_info = $db_sina_users->get_sina_user_info_item_by_sina_uid($admin_uid);
	$admin_oauth_token = $admin_info["access_token"];
	$admin_oauth_token_secret = $admin_info["token_secret"];
	$admin_token = new WeiboClient( WB_AKEY , WB_SKEY , $admin_oauth_token , $admin_oauth_token_secret );
	$admin_link ="http:\\answer.php$res";
	$admin_text = "@$answer_nick @$request_nick 要你感神马勒11。。$admin_link";
	$send_res = $admin_token->update($admin_text);
	
	if($send_res["error_code"])
	{
		throw new Exception($send_res["error"]);
	}
	

	
}
try {
	
	check_cgi_pro();
	get_data();
	$g_show["error"] = 0;
	$g_show["errmsg"] = "";
	
	
} 
catch (Exception $e) 
{
	$g_show["error"] = 1;
	$g_show["errmsg"] = $e->getMessage();
	
}
show_pro();

exit;

function show_pro()
{
	global $g_cgival, $g_pro, $g_show, $config;

	echo json_encode($g_show);

	unset($g_cgival);
	unset($g_pro);
	unset($g_show);
	unset($config);
}



?>